/*
 * Copyright (c) 2010 Philip Foulkes <philip.foulkes@gmail.com>
 *
 * This file is free software and is made available under the 
 * terms of the GNU General Public License, version 2, or at your
 * option, any later version
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 */

#ifndef _MRP_APPLICANT_H_
#define _MRP_APPLICANT_H_

/**
*   The Applicant state machine distinguishes between Active Participants (that have sent a
*   message or messages to make a declaration), Passive Participants (that require registration, but have not had
*   to declare the attribute so far to register it, and will not have to explicitly deregister), and Observers (that do
*   not require registration at present, but track the attribute's registration in case they do and become Passive
*   Participants). The
*   
*   The Applicant for each Attribute implements states that record whether it wishes to make a new declaration,
*   to maintain or withdraw an existing declaration, or has no declaration to make. It also records whether it has
*   actively made a declaration, or has been passive, taking advantage of or simply observing the declarations of
*   others
*   
*   Applicant and Simple-Applicant states
*/

#define MRP_APPLICANT_STATE_MIN		45  // This needs to be set to the value of the first item in this list 
#define MRP_APPLICANT_STATE_VO		45  // Very anxious Observer
#define MRP_APPLICANT_STATE_VP		46  // Very anxious Passive
#define MRP_APPLICANT_STATE_VN		47  // Very anxious New
#define MRP_APPLICANT_STATE_AN		48  // Anxious New
#define MRP_APPLICANT_STATE_AA		49  // Anxious Active
#define MRP_APPLICANT_STATE_QA		50  // Quiet Active
#define MRP_APPLICANT_STATE_LA		51  // Leaving Active
#define MRP_APPLICANT_STATE_AO		52  // Anxious Observer
#define MRP_APPLICANT_STATE_QO		53  // quiet Observer
#define MRP_APPLICANT_STATE_AP		54  // anxious Passive
#define MRP_APPLICANT_STATE_QP		55 // Quiet Passive
#define MRP_APPLICANT_STATE_LO		56 // Leaving Observer
#define MRP_APPLICANT_STATE_MAX		56 // This needs to be set to the value of the last item in this list 
#define MRP_APPLICANT_STATE_NO_CHANGE 57 // This needs to be one greater than MRP_APPLICANT_STATE_MAX
#define MRP_APPLICANT_STATE_NUM		(MRP_APPLICANT_STATE_MAX - MRP_APPLICANT_STATE_MIN + 1)

/**
*   An overall control parameter for each Applicant state machine, the Applicant Administrative Control,
*   determines whether or not the Applicant state machine participates in MRP protocol exchanges.
*/
#define MRP_APPLICANT_ADMINISTRATIVE_CONTROL_MIN				0 
#define MRP_APPLICANT_ADMINISTRATIVE_CONTROL_NORMAL_PARTICIPANT 0 /* The state machine participates normally in MRP protocol exchanges. */
#define MRP_APPLICANT_ADMINISTRATIVE_CONTROL_NON_PARTICIPANT	1 /* The state machine does not send any MRP messages. */
#define MRP_APPLICANT_ADMINISTRATIVE_CONTROL_MAX				1 

#include "mrp_state_machine.h"

/**
*   The job of the Applicant is twofold:
*   a) To ensure that this Participant's declaration is correctly registered by other Participants' Registrars.
*   b) To prompt other Participants to re-register after one withdraws a declaration.
*   NOTE 1) Applicant-Only implementations are concerned only with item a).
*/
struct mrp_applicant
{
	struct mrp_state_machine mrp_state_machine;  /* this has to be the first member */
	
	struct mrp_attribute * mrp_attribute;
	
	int administrative_control;
};

struct mrp_applicant * mrp_applicant_create_and_init (struct mrp_attribute * mrp_attribute);
void mrp_applicant_destroy (struct mrp_applicant ** mrp_applicant);

int mrp_applicant_set_applicant_administrative_control (struct mrp_applicant * mrp_applicant, int applicant_administrative_control);

#endif
